package kr.saeildamil.androidforstudy2.ch22;

import java.util.ArrayList;

import android.content.Context;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

public class SurfaceViewTest extends SurfaceView implements Callback {
	ArrayList<Ball> arBall;
	final static int DELAY = 50;
	SurfaceHolder mHolder;
	DrawThread mThread;
	
	public SurfaceViewTest(Context context){
		super(context);
		arBall = new ArrayList<Ball>();
		mHolder = getHolder();
		mHolder.addCallback(this);
	}
	
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
		if(mThread != null){
			mThread.SizeChange(width, height);
		}
	}
	
	public boolean onTouchEvent(MotionEvent event){
		if(event.getAction() == MotionEvent.ACTION_DOWN){
			synchronized (mHolder) {
			Ball B = new Ball((int)event.getX(), (int)event.getY());
			arBall.add(B);
			}
			return true;
		}
		return false;
		
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		mThread = new DrawThread(mHolder, arBall);
		mThread.start();

	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		mThread.bExit = true;
		for(;;){
			try {
				mThread.join();
				break;
			} catch (Exception e) {}
		}
	}
}
